package com.ujiuye.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.service.Homework01Service;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/homework01Servlet")
public class Homework01Servlet extends BaseServlet{
    private Homework01Service homework01Service = new Homework01Service();
    /**
     * 模糊搜索
     * @param req
     * @param resp
     */
    public void dimQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String search = req.getParameter("search");
        List<Object[]> lists =  homework01Service.dimQuery(search);


        //将list集合转成json
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(lists);
        resp.getWriter().print(json);
    }

    /**
     * 验证用户名唯一
     * @param req
     * @param resp
     */
    public void unameIsUnique(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String uname = req.getParameter("uname");
        int i = homework01Service.unameIsUnique(uname);

        //i > 0 true 查询的到  不让注册     i = 0 false 查不到  让注册
        resp.getWriter().print(i > 0);
    }
}
